package com.besttone.widget;

import com.besttone.search.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class EditSearchBar extends LinearLayout implements TextWatcher,
		View.OnClickListener, TextView.OnEditorActionListener {
	private static final int CMD_CHANGED = 1;
	private static final long DEALY = 500L;
	private MSCButton mBtnMsc;
	private ImageButton mClear;
	private Handler mDelayHandler = new Handler() {
		public void handleMessage(Message paramMessage) {
			if (paramMessage.what == 1) {
				String str = (String) paramMessage.obj;
				if (EditSearchBar.this.mListener != null)
					EditSearchBar.this.mListener.onKeywordChanged(str);
			}
		}
	};
	private EditText mEdit;
	private OnKeywordChangeListner mListener;
	private OnStartSearchListner mStartListener;

	public EditSearchBar(Context paramContext) {
		super(paramContext);
	}

	public EditSearchBar(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	private void publishKeywordChange(String paramString) {
		this.mDelayHandler.removeMessages(1);
		this.mDelayHandler.sendMessageDelayed(
				this.mDelayHandler.obtainMessage(1, paramString), 500L);
	}

	public void afterTextChanged(Editable paramEditable) {
	}

	public void beforeTextChanged(CharSequence paramCharSequence,
			int paramInt1, int paramInt2, int paramInt3) {
	}

	public String getKeyword() {
		String str;
		if (this.mEdit != null) {
			str = this.mEdit.getText().toString().trim();
			if (TextUtils.isEmpty(str))
				this.mEdit.setText("");
		}
		while (true) {
			str = "";
		}
	}

	public void onClick(View paramView) {
		if ((paramView == this.mClear) && (this.mEdit != null)) {
			this.mEdit.setText("");
			publishKeywordChange("");
		}
	}

	public boolean onEditorAction(TextView paramTextView, int paramInt,
			KeyEvent paramKeyEvent) {
		String str = getKeyword();
		if ((this.mStartListener != null) && (!TextUtils.isEmpty(str))) {
			this.mStartListener.onStartSearch(str);
			return true;
		}
		return false;
	}

	protected void onFinishInflate() {
		super.onFinishInflate();
		this.mEdit = ((EditText) findViewById(R.id.search_edit));
		this.mEdit.addTextChangedListener(this);
		this.mEdit.setOnEditorActionListener(this);
		new InputFilter() {
			private static final String FORBID_CHARS = " ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
			private StringBuilder sb = new StringBuilder();

			public CharSequence filter(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, Spanned paramSpanned,
					int paramInt3, int paramInt4) {
				String str;
				if (paramInt1 == paramInt2)
					str = null;
				while (true) {
					this.sb.setLength(0);
					for (int i = paramInt1;; i++) {
						if (i >= paramInt2) {
							if (this.sb.length() <= 0)
								break;
							str = this.sb.toString();
							break;
						}
						char c = paramCharSequence.charAt(i);
						if (" ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/".indexOf(c) >= 0)
							continue;
						this.sb.append(c);
					}
					str = "";
				}
			}
		};
		this.mClear = ((ImageButton) findViewById(R.id.search_clear));
		this.mClear.setOnClickListener(this);
	}

	public void onTextChanged(CharSequence paramCharSequence, int paramInt1,
			int paramInt2, int paramInt3) {
		if (TextUtils.isEmpty(paramCharSequence)) {
			this.mClear.setVisibility(View.GONE);
			publishKeywordChange(paramCharSequence.toString());
		} else {
			while (true) {
				publishKeywordChange(paramCharSequence.toString());
				this.mClear.setVisibility(View.VISIBLE);
				return;
			}
		}

	}

	public void onVoiceKeyword(String paramString) {
		if ((this.mStartListener != null) && (!TextUtils.isEmpty(paramString)))
			this.mStartListener.onStartSearch(paramString);
	}

	public void setHint(int paramInt) {
		if (this.mEdit != null)
			this.mEdit.setHint(paramInt);
	}

	public void setKeyword(String paramString) {
		if (paramString == null) {
			while (true) {
				if ((this.mEdit != null)
						&& (paramString.compareTo(this.mEdit.getText()
								.toString()) != 0)) {
					this.mEdit.setText(paramString);
					this.mEdit.setSelection(-1 + paramString.length());
					continue;
				}
				return;
			}

		}
	}

	public void setOnKeywordChangeListner(
			OnKeywordChangeListner paramOnKeywordChangeListner) {
		this.mListener = paramOnKeywordChangeListner;
	}

	public void setOnStartSearchListner(
			OnStartSearchListner paramOnStartSearchListner) {
		this.mStartListener = paramOnStartSearchListner;
	}

	public static abstract interface OnKeywordChangeListner {
		public abstract void onKeywordChanged(String paramString);
	}

	public static abstract interface OnStartSearchListner {
		public abstract void onStartSearch(String paramString);
	}
}
